package com.xydtech.system.service.impl;

import com.xydtech.common.core.domain.Ztree;
import com.xydtech.common.core.text.Convert;
import com.xydtech.system.domain.FivelevelAddr;
import com.xydtech.system.mapper.FivelevelAddrMapper;
import com.xydtech.system.service.IFivelevelAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 五级行政区划Service业务层处理
 *
 * @author JHON
 * @date 2021-07-11
 */
@Service("fiveLevelAddr")
public class FivelevelAddrServiceImpl implements IFivelevelAddrService {
    @Autowired
    private FivelevelAddrMapper fivelevelAddrMapper;

    /**
     * 查询五级行政区划
     *
     * @param code 五级行政区划ID
     * @return 五级行政区划
     */
    @Override
    public FivelevelAddr selectSysFivelevelAddrById(String code) {
        return fivelevelAddrMapper.selectSysFivelevelAddrById(code);
    }

    /**
     * 查询五级行政区划
     *
     * @param upCode 五级行政区划父级ID
     * @return 五级行政区划
     */
    @Override
    public List<FivelevelAddr> getAddrList(String upCode) {
        return fivelevelAddrMapper.selectSysFivelevelAddrByUpCode(upCode);
    }

    /**
     * 查询五级行政区划列表
     *
     * @param fivelevelAddr 五级行政区划
     * @return 五级行政区划
     */
    @Override
    public List<FivelevelAddr> selectSysFivelevelAddrList(FivelevelAddr fivelevelAddr) {
        return fivelevelAddrMapper.selectSysFivelevelAddrList(fivelevelAddr);
    }

    /**
     * 新增五级行政区划
     *
     * @param fivelevelAddr 五级行政区划
     * @return 结果
     */
    @Override
    public int insertSysFivelevelAddr(FivelevelAddr fivelevelAddr) {
        //查询相同父级下最大编码
        FivelevelAddr addr = fivelevelAddrMapper.selectMaxCodeByUpCode(fivelevelAddr.getUpCode());
        StringBuilder code = new StringBuilder();
        if (fivelevelAddr.getpLevel() == 2) {
            code.append(addr.getCode().substring(0, 2)).
                    append(String.format("%0" + 2 + "d", Integer.parseInt(addr.getCode().substring(2, 4)) + 1))
                    .append("00000000");
            fivelevelAddr.setCode(code.toString());
        } else if (fivelevelAddr.getpLevel() == 3) {
            code.append(addr.getCode().substring(0, 4)).
                    append(String.format("%0" + 2 + "d", Integer.parseInt(addr.getCode().substring(4, 6)) + 1))
                    .append("000000");
            fivelevelAddr.setCode(code.toString());
        } else if (fivelevelAddr.getpLevel() == 4) {
            code.append(addr.getCode().substring(0, 6)).
                    append(String.format("%0" + 3 + "d", Integer.parseInt(addr.getCode().substring(6, 9)) + 1))
                    .append("000");
            fivelevelAddr.setCode(code.toString());
        } else if (fivelevelAddr.getpLevel() == 5) {
            code.append(addr.getCode().substring(0, 9)).
                    append(String.format("%0" + 3 + "d", Integer.parseInt(addr.getCode().substring(9, 12)) + 1));
            fivelevelAddr.setCode(code.toString());
        }
        fivelevelAddr.setAncestors(addr.getAncestors());
        return fivelevelAddrMapper.insertSysFivelevelAddr(fivelevelAddr);
    }

    /**
     * 修改五级行政区划
     *
     * @param fivelevelAddr 五级行政区划
     * @return 结果
     */
    @Override
    public int updateSysFivelevelAddr(FivelevelAddr fivelevelAddr) {
        return fivelevelAddrMapper.updateSysFivelevelAddr(fivelevelAddr);
    }

    /**
     * 删除五级行政区划对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysFivelevelAddrByIds(String ids) {
        return fivelevelAddrMapper.deleteSysFivelevelAddrByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除五级行政区划信息
     *
     * @param code 五级行政区划ID
     * @return 结果
     */
    @Override
    public int deleteSysFivelevelAddrById(String code) {
        return fivelevelAddrMapper.deleteSysFivelevelAddrById(code);
    }

    /**
     * 查询五级行政区划树列表
     *
     * @return 所有五级行政区划信息
     */
    @Override
    public List<Ztree> selectSysFivelevelAddrTree(String code) {
        FivelevelAddr levelAddr = new FivelevelAddr();
        levelAddr.setCode(code);
        List<FivelevelAddr> fivelevelAddrList = fivelevelAddrMapper.selectSysFivelevelAddrTree(levelAddr);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (FivelevelAddr fivelevelAddr : fivelevelAddrList) {
            Ztree ztree = new Ztree();
            ztree.setId(Long.parseLong(fivelevelAddr.getCode()));
            ztree.setpId(Long.parseLong(fivelevelAddr.getUpCode()));
            ztree.setName(fivelevelAddr.getName());
            ztree.setTitle(fivelevelAddr.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

}
